package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.entity.StudentContactInfo;
import com.rocksdata.mc.entity.StudentInfo;
import com.rocksdata.mc.Constant;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by 陈堂正 on 2017/4/27.
 */
@ContentView(R.layout.activity_teacher_relationship)
public class TeacherRelationshipActivity extends TitleBarActivity {
    @ViewInject(R.id.lv_teacher_relationship)
    private ExpandableListView lvTeacherRelationship;

    private List<StudentContactInfo> classList;
    private ClassListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleMiddleText("相关班级");

        classList = (List<StudentContactInfo>) getIntent().getSerializableExtra("classes");
        BusinessInterface.IUserInfoManager manager = BusinessManager.getUserInfoManager();
        for(StudentContactInfo info: classList){
            info.setStudents(manager.getStudentListByClassNum(info.getClassCode()));
        }
        adapter = new ClassListAdapter(this, classList);
        lvTeacherRelationship.setAdapter(adapter);
        lvTeacherRelationship.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                StudentInfo userInfo = adapter.getChild(groupPosition, childPosition);
                Intent intent = new Intent(TeacherRelationshipActivity.this, StudentDetailActivity.class);
                intent.putExtra(Constant.KEY_USER_ID, userInfo.getStudentCode());
                startActivity(intent);
                return true;
            }
        });
    }

    private class ClassListAdapter extends BaseExpandableListAdapter {
        private Context context;
        private List<StudentContactInfo> dataList;

        public ClassListAdapter(Context context, List<StudentContactInfo> dataList) {
            this.context = context;
            this.dataList = dataList;
        }

        @Override
        public int getGroupCount() {
            return dataList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            List<StudentInfo> userList = dataList.get(groupPosition).getStudents();
            int count = 0;
            if (userList != null) {
                count = userList.size();
            }
            return count;
        }

        @Override
        public StudentContactInfo getGroup(int groupPosition) {
            return dataList.get(groupPosition);
        }

        @Override
        public StudentInfo getChild(int groupPosition, int childPosition) {
            return dataList.get(groupPosition).getStudents().get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            final StudentContactInfo info = getGroup(groupPosition);
            final GroupViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_teacher_relationship_list_group, null);
                holder = new GroupViewHolder();
                holder.ivFlag = (ImageView) convertView.findViewById(R.id.iv_expanded_flag);
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tvLabel = (TextView) convertView.findViewById(R.id.tv_teacher_label);
                convertView.setTag(holder);
            } else {
                holder = (GroupViewHolder) convertView.getTag();
            }

            holder.tvLabel.setText(info.getLabel());

            // 设置箭头状态
            if (isExpanded) {
                holder.ivFlag.setImageResource(R.mipmap.ic_indicator_expanded);
            } else {
                holder.ivFlag.setImageResource(R.mipmap.ic_indicator_unexpanded);
            }
            holder.tvName.setText(info.getClassName());
            return convertView;
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                                 ViewGroup parent) {
            final StudentInfo info = getChild(groupPosition, childPosition);
            final ChildViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_contact_list_child, null);
                holder = new ChildViewHolder();
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                convertView.setTag(holder);
            } else {
                holder = (ChildViewHolder) convertView.getTag();
            }

            holder.tvName.setText(info.getStudentName());
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        class GroupViewHolder {
            ImageView ivFlag;
            TextView tvName;
            TextView tvLabel;
        }

        class ChildViewHolder {
            TextView tvName;
        }
    }
}
